﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Linq;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1._Controls
{
	[ValidationProperty("Values")]
	public partial class DicCheckBoxList : System.Web.UI.UserControl
	{
		private LoadedValue<int?> loadedSiteId;
		public int? SiteId
		{
			get
			{
				if (loadedSiteId.Loader == null)
					loadedSiteId.Loader = () => (int?)ViewState["SiteId"];
				return loadedSiteId.Value;
			}
			set
			{
				if (loadedSiteId.Setter == null)
					loadedSiteId.Setter = v =>
					{
						loadedClassId.ResetValue();
						ViewState["SiteId"] = v;
					};
				loadedSiteId.Value = value;
			}
		}

		private LoadedValue<string> loadedClassCode;
		public string ClassCode
		{
			get
			{
				if (loadedClassCode.Loader == null)
					loadedClassCode.Loader = () => (string)ViewState["ClassCode"];
				return loadedClassCode.Value;
			}
			set
			{
				if (loadedClassCode.Setter == null)
					loadedClassCode.Setter = v =>
					{
						loadedClassId.ResetValue();
						ViewState["ClassCode"] = v;
					};
				loadedClassCode.Value = value;
			}
		}

		private LoadedValue<int> loadedClassId;
		protected int ClassId
		{
			get
			{
				if (loadedClassId.Loader == null)
					loadedClassId.Loader = () => { return DicClasses.GetIdentifier(SiteId, ClassCode).Value; };
				return loadedClassId.Value;
			}
		}

		private LoadedValue<DicValueType> loadedValueType;
		public DicValueType ValueType
		{
			get
			{
				if (loadedValueType.Loader == null)
					loadedValueType.Loader = () => EnumExtensions.ConvertFromOrDefault<DicValueType>(ViewState["ValueType"], DicValueType.DicId);
				return loadedValueType.Value;
			}
			set
			{
				if (loadedValueType.Setter == null)
					loadedValueType.Setter = v => ViewState["ValueType"] = v;
				loadedValueType.Value = value;
			}
		}

		public RepeatDirection RepeatDirection
		{
			get { return CtrList.RepeatDirection; }
			set { CtrList.RepeatDirection = value; }
		}

		public int RepeatColumns
		{
			get { return CtrList.RepeatColumns; }
			set { CtrList.RepeatColumns = value; }
		}

		public RepeatLayout RepeatLayout
		{
			get { return CtrList.RepeatLayout; }
			set { CtrList.RepeatLayout = value; }
		}

		public string[] Values
		{
			get
			{
				List<string> values = new List<string>();
				foreach (ListItem item in CtrList.Items)
					if (item.Selected) values.Add(item.Value);
				return values.ToArray();
			}
			set
			{
				CtrList.DataBind();
				foreach (ListItem item in CtrList.Items)
					item.Selected = value.Contains(item.Value);
			}
		}

		private bool isBindCtrList = false;
		protected void CtrList_DataBinding(object sender, EventArgs e)
		{
			if (isBindCtrList) return;
			CtrList.DataSource = Dictionaries.GetItems(ClassId, null);
			CtrList.DataTextField = "Name";
			CtrList.DataValueField = ValueType == DicValueType.Value ? "Value" : "DicId";
		}

		protected void CtrList_DataBound(object sender, EventArgs e)
		{
			CtrList.DataSource = null;
			isBindCtrList = true;
		}
	}
}